<script setup>
import {reactive, ref} from 'vue';
import {User, Lock} from '@element-plus/icons-vue'
const researchers = ref({
   name : '',
  password:'',
  captcha:''
})

//login
import {useTokenStore} from "@/store/token.js";
const tokenStore = useTokenStore()
import {researchersApi} from "@/api/researchers.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const login = () => {
  ElMessage.info(researchers.value.name)
  researchersApi.login(researchers.value).then( result => {
    console.log(result)
    if (result.code == 0) {
      ElMessage.success(result.msg);
      //将得到的token存到pinia中(⭐)
      tokenStore.setToken(result.data)
      router.push('/')
    } else {
      ElMessage.error(result.msg)
    }
  })
};
//用于注册的表单校验模型
const rules = ref({
  name: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 5, max: 16, message: '用户名的长度必须为5~16位', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 3, max: 16, message: '密码长度必须为3~16位', trigger: 'blur'}
  ]
})

//验证码

import captchaApi from "@/api/captcha.js";
const captcha = ref('')
const loadCaptcha = () => {
  captchaApi.captcha().then(result => {
    captcha.value = result.data
  })
}

loadCaptcha()

</script>

<template>

    <div class="login-container">
      <div class="login-box">
        <h1>竞赛管理平台</h1>
        <el-form :rules="rules" :model="researchers">
<!--        <input type="text" v-model="researchers.name" placeholder="用户名" style="width: 90%"/>-->
          <el-form-item prop="name">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="researchers.name"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                      v-model="researchers.password"></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input name="captcha" :prefix-icon="Lock" type="text" placeholder="请输入验证码"
                      v-model="researchers.captcha"></el-input>
          </el-form-item>
          <img width="160px" :src="captcha" @click="loadCaptcha">
          <el-form-item class="flex">
            <div class="flex">
              <el-checkbox v-model="remember">记住我</el-checkbox>
              <el-link type="primary" :underline="false">忘记密码？</el-link>
            </div>
          </el-form-item>
<!--        <input type="password" v-model="researchers.password" placeholder="密码" style="width: 90%"/>-->
        </el-form>
        <button @click="login">登录</button>
      </div>
    </div>


</template>

<style scoped>
.login-container {
  background-color: #001529; /* 深蓝色背景 */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.1); /* 半透明白色背景 */
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(51, 153, 255, 0.5); /* 蓝色阴影 */
  color: #fff;
  width: 300px;
}

h1 {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 0 5px rgba(51, 153, 255, 0.8); /* 蓝色文字阴影 */
}

input {
  width: 100%;
  padding: 10px;
  margin-bottom: 15px;
  border: none;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
  transition: background-color 0.3s ease;
}

input:focus {
  background-color: rgba(255, 255, 255, 0.2);
  outline: none;
}

button {
  width: 100%;
  padding:  10px;
  background-color: #3399ff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #66b3ff;
}

/* 添加一些未来感的线条特效 */
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: repeating-linear-gradient(
      45deg,
      rgba(51, 153, 255, 0.3),
      rgba(51, 153, 255, 0.3) 10px,
      rgba(255, 255, 255, 0.1) 10px,
      rgba(255, 255, 255, 0.1) 20px
  );
  z-index: -1;
}
</style>